package com.hy.frame.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.hy.frame.widget.ui.R;

public class DesignTextHelper extends DesignHelper {
    private int drawLeftWidth;
    private int drawLeftHeight;
    private int drawTopWidth;
    private int drawTopHeight;
    private int drawRightWidth;
    private int drawRightHeight;
    private int drawBottomWidth;
    private int drawBottomHeight;
    private int drawPadding;
    private boolean drawCenter = false;

    public DesignTextHelper(@NonNull View target) {
        super(target);
    }

    public void loadTextAttributes(AttributeSet attrs, int defStyleAttr) {
        if (mTarget == null || mTarget.get() == null) return;
        TextView v = (TextView) mTarget.get();
        Context context = v.getContext();
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.IDesignTextView, defStyleAttr, 0);
        int textSize = a.getDimensionPixelSize(R.styleable.IDesignTextView_designTextSize, 0);
        boolean scaleDefaultSize = a.getBoolean(R.styleable.IDesignTextView_designScaleDefaultSize, true);
        this.drawLeftWidth = calDesignSize(a.getDimensionPixelSize(R.styleable.IDesignTextView_designDrawLeftWidth, 0));
        this.drawLeftHeight = calDesignSize(a.getDimensionPixelSize(R.styleable.IDesignTextView_designDrawLeftHeight, 0), this.drawLeftWidth);
        this.drawTopWidth = calDesignSize(a.getDimensionPixelSize(R.styleable.IDesignTextView_designDrawTopWidth, 0));
        this.drawTopHeight = calDesignSize(a.getDimensionPixelSize(R.styleable.IDesignTextView_designDrawTopHeight, 0), this.drawTopWidth);
        this.drawRightWidth = calDesignSize(a.getDimensionPixelSize(R.styleable.IDesignTextView_designDrawRightWidth, 0));
        this.drawRightHeight = calDesignSize(a.getDimensionPixelSize(R.styleable.IDesignTextView_designDrawRightHeight, 0), this.drawRightWidth);
        this.drawBottomWidth = calDesignSize(a.getDimensionPixelSize(R.styleable.IDesignTextView_designDrawBottomWidth, 0));
        this.drawBottomHeight = calDesignSize(a.getDimensionPixelSize(R.styleable.IDesignTextView_designDrawBottomHeight, 0), this.drawBottomWidth);
        this.drawPadding = calDesignSize(a.getDimensionPixelSize(R.styleable.IDesignTextView_designDrawPadding, 0));
        this.drawCenter = a.getBoolean(R.styleable.IDesignTextView_designDrawCenter, false);
        a.recycle();
        if (textSize == 0 && scaleDefaultSize)
            textSize = (int) v.getTextSize();
        if (textSize > 0)
            v.setTextSize(TypedValue.COMPLEX_UNIT_PX, calDesignSize(textSize));
        if (drawPadding > 0)
            v.setCompoundDrawablePadding(drawPadding);
        Drawable[] drawables = v.getCompoundDrawables();
        v.setCompoundDrawables(drawables[0], drawables[1], drawables[2], drawables[3]);
    }

    public void setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom) {
        if (drawLeftWidth > 0 && left != null)
            left.setBounds(0, 0, drawLeftWidth, drawLeftHeight);
        if (drawTopWidth > 0 && top != null)
            top.setBounds(0, 0, drawTopWidth, drawTopHeight);
        if (drawRightWidth > 0 && right != null)
            right.setBounds(0, 0, drawRightWidth, drawRightHeight);
        if (drawBottomWidth > 0 && bottom != null)
            bottom.setBounds(0, 0, drawBottomWidth, drawBottomHeight);
    }

    public void setDrawLeftWidth(int drawLeftWidth) {
        this.drawLeftWidth = drawLeftWidth;
    }

    public void setDrawLeftHeight(int drawLeftHeight) {
        this.drawLeftHeight = drawLeftHeight;
    }

    public void setDrawTopWidth(int drawTopWidth) {
        this.drawTopWidth = drawTopWidth;
    }

    public void setDrawTopHeight(int drawTopHeight) {
        this.drawTopHeight = drawTopHeight;
    }

    public void setDrawRightWidth(int drawRightWidth) {
        this.drawRightWidth = drawRightWidth;
    }

    public void setDrawRightHeight(int drawRightHeight) {
        this.drawRightHeight = drawRightHeight;
    }

    public void setDrawBottomWidth(int drawBottomWidth) {
        this.drawBottomWidth = drawBottomWidth;
    }

    public void setDrawBottomHeight(int drawBottomHeight) {
        this.drawBottomHeight = drawBottomHeight;
    }

    public void setDrawPadding(int drawPadding) {
        this.drawPadding = drawPadding;
        TextView v = (TextView) mTarget.get();
        if (drawPadding > 0 && v !=null)
            v.setCompoundDrawablePadding(drawPadding);
    }

    public void setDrawCenter(boolean drawCenter) {
        this.drawCenter = drawCenter;
    }

    public void onDraw(Canvas canvas) {
        if (mTarget == null || mTarget.get() == null) return;
        TextView v = (TextView) mTarget.get();
        if (v.getText() == null) return;
        String text = v.getText().toString();
        Drawable[] drawables = v.getCompoundDrawables();
        Drawable drawableLeft = drawables[0];
        if (drawCenter && drawableLeft != null) {
            float textWidth = v.getPaint().measureText(text);
            int drawablePadding = v.getCompoundDrawablePadding();
            if (this.drawPadding > 0) drawablePadding = drawPadding;
            int drawableWidth = drawLeftWidth;
            float bodyWidth = textWidth + drawableWidth + drawablePadding;
            canvas.translate((v.getMeasuredWidth() - bodyWidth) / 2 - v.getPaddingLeft(), 0f);
        }
    }

    /**
     * 尺寸转换默认不转换
     *
     * @param size 原尺寸
     * @param def  默认值
     * @return 转换后的尺寸
     */
    public int calDesignSize(int size, int def) {
        int value = calDesignSize(size);
        if (value == 0)
            return def;
        return value;
    }

    static class TextViewStyle {
        int textSize = 0;
        int drawLeftWidth = 0;
        int drawLeftHeight = 0;
        int drawTopWidth = 0;
        int drawTopHeight = 0;
        int drawRightWidth = 0;
        int drawRightHeight = 0;
        int drawBottomWidth = 0;
        int drawBottomHeight = 0;
        int drawPadding = 0;
        boolean drawCenter = false;
    }
}
